
<style lang="scss" scoped>
.taglist {
  .main {
    width: 76%;
    margin: 0 12%;
    .one {
      height: 40px;
      line-height: 40px;
      background-color: rgb(243, 243, 243);
    }
    .two {
      height: 40px;
      line-height: 40px;
      background-color: rgb(243, 243, 243);
      color: gray;
    }
    .four {
      margin-top: 20px;
      height: 40px;
      line-height: 40px;
      background-color: rgb(243, 243, 243);
    }
  }
}
</style>
<template>
  <div class="taglist">
    <div class="main">
      <div style="one"><el-button @click="dialog()">+创建标签</el-button></div>
      <div class="two"><i class="el-icon-s-operation"></i> 数据列表</div>
      <div class="three">
        <template>
          <el-table :data="List" style="width: 100%">
            <el-table-column prop="name" label="商品名称" width="180">
            </el-table-column>
            <el-table-column prop="pic" label="标签图片" width="180">
              <template slot-scope="scope">
                <img :src="scope.row.pic" style="width: 50px; height: 50px" />
              </template>
            </el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button @click="changetag(scope.row)">编辑</el-button>
                <el-button @click="deltag(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 新增 -->
        <el-dialog title="创建标签" :visible.sync="dialogVisible" width="60%">
          <el-form>
            <el-form-item label="标签名称:">
              <el-input v-model="addtag.name"> </el-input>
            </el-form-item>
            <el-form-item label="商品名称:">
              <!-- 上传图片 -->
              <el-upload
                :limit="1"
                list-type="picture-card"
                :file-list="fileList"
                :on-change="onChange"
                action="http://dida100.com:8888/api/file/upload"
              >
                <i slot="default" class="el-icon-plus"></i>
                <div slot="file" slot-scope="{ file }">
                  <img
                    class="el-upload-list__item-thumbnail"
                    :src="file.url"
                    alt=""
                  />
                  <!-- 设置主图 -->
                  <span class="el-upload-list__item-actions">
                    <!-- 移除当前点击图片 -->
                    <span
                      class="el-upload-list__item-delete"
                      @click="handleRemove(file)"
                    >
                      <i class="el-icon-delete"></i>
                    </span>
                  </span>
                </div>
              </el-upload>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="add()">保存</el-button>
          </span>
        </el-dialog>

        <!-- 编辑标签 -->
        <el-dialog title="创建标签" :visible.sync="visibleShow" width="60%">
          <el-form>
            <el-form-item label="商品名称:">
              <el-input v-model="addtag.name"> </el-input>
            </el-form-item>
            <el-form-item label="标签图片:">
              <!-- 上传图片 -->
              <el-upload
                :limit="1"
                list-type="picture-card"
                :file-list="fileList"
                :on-change="onChange"
                action="http://dida100.com:8888/api/file/upload"
              >
                <i slot="default" class="el-icon-plus"></i>
                <div slot="file" slot-scope="{ file }">
                  <img
                    class="el-upload-list__item-thumbnail"
                    :src="file.url"
                    alt=""
                  />
                  <!-- 设置主图 -->
                  <span class="el-upload-list__item-actions">
                    <!-- 移除当前点击图片 -->
                    <span
                      class="el-upload-list__item-delete"
                      @click="handleRemove(file)"
                    >
                      <i class="el-icon-delete"></i>
                    </span>
                  </span>
                </div>
              </el-upload>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="change()">保存</el-button>
          </span>
        </el-dialog>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination
.currentPage"
          :page-size="pagination
.pageSize"
          :page-sizes="[5, 10, 15, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination
.total"
        >
        </el-pagination>
      </div>
      <div class="four"></div>
    </div>
  </div>
</template>
<script>
import { getTag, delTag, addTag,  } from "@/api/assetsdata";
export default {
  data() {
    return {
      addtag: {
        pic: "",
        name: "",
        type: "1",
      },
      dialogVisible: false,
      visibleShow: false,
      List: [],
      pagination
: {},
      search: {
        size: 10,
        current: 1,
      },
      //   图片
      fileList: [],
    };
  },
  created() {
    this.gegtag();
  },
  methods: {
      change() {
        this.addtag.name =""
        this.visibleShow = false;
   
    },
    changetag(e) {
      this.visibleShow = true;
      this.addtag.name = e.name;
    },
    add() {
      this.addtag.pic =
        "http://dida100.com:8888" + this.fileList[0].response.file.path;
      this.dialogVisible = false;
      addTag(this.addtag).then((res) => {
        if (res.data.code == 0) {
          this.$message({
            type: "success",
            message: "添加成功!",
          });
          this.addtag.pic = "";
          this.addtag.name = "";
          this.fileList = [];
          this.gegtag();
        }
      });
    },
    onChange(file, filelist) {
      this.fileList = filelist;
      //
    },
    // 移除图片
    handleRemove() {
      this.fileList.splice(0, 1);
    },

    dialog() {
      this.dialogVisible = true;
    },
    deltag(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delTag(id).then((res) => {
            if (res && res.data && res.data.code === 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.gegtag();
            } else {
              this.$message({
                type: "success",
                message: res.data.msg || "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    gegtag() {
      getTag(this.search).then((res) => {
        this.List = res.data.data;
        this.pagination
 = res.data.pagination;
        // console.log(this.pagination);
        this.pagination
.currentPage = this.pagination
.currentPage * 1;
        this.pagination
.pageSize = this.pagination
.pageSize * 1;
        console.log(this.List);
      });
    },

    // 分页发生变化
    handleSizeChange(val) {
      this.search.size = val;
      this.gegtag();
    },
    // 处理current变化
    handleCurrentChange(val) {
      // 当当前页发生变化时候更新 搜索条件的当前页
      // 获取数据
      this.search.current = val;
      this.gegtag();
    },
  },
};
</script>